/**
 * created by chan on 2016-09-01.
 */
var main = {};
(function () {
	
	main.defaultCfg = {
		path:''
	};
		
	main.init = function (cfg) {
        var self = this;
        main.cfg = $.extend(main.defaultCfg, cfg);
    };
    
    //生成菜单树
    main.generateTree = function(menuParent, menuChild){

		/** ---- ajax start ----*/
		$.ajax({ 
			url:main.cfg.path+"/manage/system/permission/tree",
			type:"post",
			success:function(data){
				
				
				var trees = data;
				var html = "";
				//数组
				if(trees.length>0){
					//循环一级菜单
					for(var i = 0; i<trees.length; i++){
						var tree = trees[i];
						
						//加载一级菜单开始
						if(tree.menuParent==menuParent){
							html+="<li class=\"active open\">";
						}else{
							html+="<li>";
						}
						html+="	<a href=\"#\" class=\"dropdown-toggle\">";
						html+="		<i class=\"menu-icon fa fa-cog\"></i>";
						html+="		<span class=\"menu-text\">"+tree.name+"</span>";
						html+="		<b class=\"arrow fa fa-angle-down\"></b>";
						html+="	</a>";
						html+="	<b class=\"arrow\"></b>";
						
						//循环二级菜单
						if(tree.children.length>0){
							html+="<ul class=\"submenu\">";
							for(var j=0;j<tree.children.length;j++){
								if(tree.children[j].menuParent==menuParent && tree.children[j].menuChild==menuChild){
									html+="	<li class=\"active\">";
								}else{
									html+="	<li>";
								}
								html+="		<a href='"+main.cfg.path+"/"+tree.children[j].url+"'>";
								html+="			<i class=\"menu-icon fa fa-caret-right\"></i>";
								html+=tree.children[j].name;
								html+="		</a>";
								html+="	</li>";
							}
							html+="</ul>";
						}
						//加载一级菜单结束
						html+="</li>";	
					}
				}
				//菜单树结束
				$("#index").after(html);
			}
		})
		/** ---- ajax end ----*/
    }
    
})(jQuery);